package com.googlecode.aleverr.yafotkiclient.ui.adapters;

import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewSwitcher;
import com.googlecode.aleverr.yafotkiclient.R;
import com.googlecode.aleverr.yafotkiclient.api.Album;
import com.googlecode.aleverr.yafotkiclient.api.Photo;

public class AlbumsListAdapter extends AbstractPhotoAdapter<Album> {

    private static final int PROGRESS_BAR_INDEX = 0;
    private static final int IMAGE_VIEW_INDEX = 1;

    @Override
    protected void processElement(View view, Album album) {
        TextView title = (TextView) view.findViewById(R.id.album_title);
        title.setText(album.getTitle());
        TextView summary = (TextView) view.findViewById(R.id.albumSummary);
        summary.setText(album.getSummary() != null ? album.getSummary() : "");
        ViewSwitcher viewSwitcher = (ViewSwitcher) view.findViewById(R.id.viewSwitcher);
        Photo coverPhoto = album.getCoverPhoto();
        if (coverPhoto != null) {
            viewSwitcher.setDisplayedChild(PROGRESS_BAR_INDEX);
            PHOTO_LOADER.queuePhotoLoad(viewSwitcher, coverPhoto, this);
        } else {
            ImageView imageView = (ImageView) view.findViewById(R.id.coverPhoto);
            imageView.setImageResource(R.drawable.no_cover);
            viewSwitcher.setDisplayedChild(IMAGE_VIEW_INDEX);
        }
    }

    @Override
    protected View loadView(ViewGroup viewGroup) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        return inflater.inflate(R.layout.albums_item, viewGroup, false);
    }

    @Override
    public void assignBitmapToView(Bitmap bitmap, View view) {
        ImageView imageView = (ImageView) view.findViewById(R.id.coverPhoto);
        if (bitmap != null) {
            imageView.setImageBitmap(bitmap);
        } else {
            imageView.setImageResource(R.drawable.no_cover);
        }
        if (view instanceof ViewSwitcher) {
            ((ViewSwitcher) view).setDisplayedChild(IMAGE_VIEW_INDEX);
        }
    }
}
